package com.bdqn.zhao.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.zhao.pojo.R;
import com.bdqn.zhao.pojo.SysRole;
import com.bdqn.zhao.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("system/role")
public class SysRoleController extends BaseController {

    @Autowired
    private SysRoleService sysRoleService;


    /**
     * 第一次访问这个页面发起的请求
     *
     * @param pageNum
     * @param pageSize
     * @param sortField
     * @param sortOrder
     * @return
     */
    @ResponseBody
    @GetMapping("/list")
    public Map<String, Object> getList(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, @RequestParam("sortField") String sortField, @RequestParam("sortOrder") String sortOrder, @RequestParam(value = "status", required = false) Integer status, @RequestParam(value = "roleName", required = false) String roleName) {
        HashMap<String, Object> map = new HashMap<>();
        Page<SysRole> sysRoles = null;
        try {
            sysRoles = sysRoleService.listt(pageNum, pageSize, sortField, sortOrder, status, roleName);
            map.put("msg", "success");
            map.put("pageNum", sysRoles.getCurrent());
            map.put("total", sysRoles.getRecords().size());
            map.put("code", 0);
            map.put("rows", sysRoles.getRecords());
        } catch (Exception e) {
            map.put("msg", "失败!");
            map.put("code", 500);
        }
        return map;
    }
    //根据姓名状态查询

    /**
     * 是否超管
     *
     * @param userId
     * @return
     */
    public boolean isAdmin(Long userId) {
        return userId != null && 1L == userId;
    }


    //@GetMapping("all")
    //@ResponseBody
    //public R all(SysRole role, HttpSession session) {
    //    String currentId = (String) session.getAttribute("currentId");
    //    if (currentId == null) {
    //        currentId = String.valueOf(0L);
    //    }
    //    if (isAdmin(Long.valueOf(currentId))) {
    //        role.setParkId(null);
    //    }
    //    return R.ok().put("rows", sysRoleService.selectRoleAll(role));
    //}


    //status

    /**
     * 修改状态
     *
     * @param id
     * @param status
     * @return
     */
    @ResponseBody
    @GetMapping("/status")
    public R updateStatus(@RequestParam("id") Integer id, @RequestParam("status") Integer status) {
        return toAjax(sysRoleService.updateStatus(id, status));
    }

    /**
     * 删除角色
     *
     * @throws Exception
     */
    @PostMapping("/remove")
    @ResponseBody
    public R remove(String ids) throws Exception {
        return toAjax(sysRoleService.deleteRoleByIds(ids));
    }

    /**
     * 新增保存角色
     */
    @PostMapping("/save")
    @ResponseBody
    public R addSave(SysRole role) {
        role.setCreateBy(getLoginName());
        return toAjax(sysRoleService.insertRole(role));
    }

    /**
     * 查询角色
     */
    @GetMapping("/get/{roleId}")
    @ResponseBody
    public SysRole get(@PathVariable("roleId") Long roleId) {
        return sysRoleService.selectRoleById(roleId);
    }

    /**
     * 修改保存角色
     */
    @PostMapping("/update")
    public R editSave(SysRole role) {
        role.setUpdateBy(getLoginName());
        return toAjax(sysRoleService.updateRole(role));
    }

    /**
     * 保存角色分配数据权限
     */
    @PostMapping("/authDataScope")
    @ResponseBody
    public R authDataScopeSave(SysRole role) {
        role.setUpdateBy(getLoginName());
        if (sysRoleService.authDataScope(role) > 0) {
            return R.ok();
        }
        return R.error();
    }


}

